/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.service.processor.active;

import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.manage.core.domain.Organization;
import info.peigen.hotpot.manage.core.domain.Position;
import info.peigen.hotpot.manage.core.domain.Role;
import info.peigen.hotpot.manage.core.domain.User;
import info.peigen.hotpot.manage.core.service.order.LoginOrder;
import info.peigen.hotpot.manage.core.service.order.UserEditOrder;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(UserActiveProcessor)</b>
 * "用户" 激活处理器
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-28
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Component
public class UserActiveProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 22;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        switchOrder(serviceContext);
    }

    private void switchOrder(ProcessorServiceContext serviceContext) {
        ServiceOrderBase contextOrder = serviceContext.getOrder();
        if (contextOrder instanceof UserEditOrder order) {
            User domain = userRepository.active(order.getUserId());

            // active Role
            List<Role> roles = (List<Role>) serviceContext.getExtend().get("roles");
            domain.setRoles(roles);

            // active Position
            List<Position> positions = (List<Position>) serviceContext.getExtend().get("positions");
            domain.setPositions(positions);

            // active Organization
            List<Organization> organizations = (List<Organization>) serviceContext.getExtend().get("organizations");
            domain.setOrganizations(organizations);

            serviceContext.setDomain(domain);
        } else if (contextOrder instanceof LoginOrder order) {
            User domain = userRepository.activeByLoginName(order.getLoginName());
            serviceContext.setDomain(domain);
        }

    }

}